/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.db.*;
import blueprint4j.utils.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;

public class DataPairFieldImage extends DataPairDefault implements FieldBinding, ActionListener {
    static private JFileChooser file_chooser = new JFileChooser();
    private BindImageInterface field;
    private boolean editable = true;
    //private JLabel image_field=null;
    private JImage image_field = null;
    private JButton choose = new JButton("Find");
    private JButton remove = new JButton("Remove");
    private JButton edit = new JButton("Edit");
    private Dimension preffered_size;
    
    static {
        file_chooser.setFileFilter(
                new FileFilter() {
            public boolean accept(java.io.File f) {
                return (f != null && (f.isDirectory() || f.getName().endsWith("jpg") || f.getName().endsWith("png") ||
                        f.getName().endsWith("bmp") || f.getName().endsWith("gif")));
            }
            public String getDescription() {return "Images";}
        }
        );
    }
    
    public DataPairFieldImage(Binder binder,BindImageInterface p_field,Dimension size,boolean editable){
        super(binder,p_field);
        this.preffered_size=size;
        this.editable = editable;
        if (p_field.get() != null) {
            //image_field=new JLabel(new JImage(p_field.get()));
            //image_field.setText(null);
            image_field = new JImage(p_field.get().getImage());
        } else {
            //image_field=new JLabel();
            image_field = new JImage(null);
        }
        image_field.setPreferredSize(preffered_size);
        field=p_field;
        setLabel(new JLabel(field.getDescription()));
        JPanel image_panel = new JPanel();
        image_panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        image_panel.add(image_field);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JPanel btn_panel = new JPanel();
        btn_panel.setLayout(new BorderLayout());
        JPanel pp = new JPanel();
        pp.setLayout(new BorderLayout());
        pp.add(choose,BorderLayout.NORTH);
        pp.add(remove,BorderLayout.CENTER);
        pp.add(edit,BorderLayout.SOUTH);
        
        btn_panel.add(pp,BorderLayout.NORTH);
        panel.add(BorderLayout.WEST,image_panel);
        panel.add(BorderLayout.EAST,btn_panel);
        setComponent(panel);
        choose.setEnabled(editable);
        choose.setEnabled(editable);
        remove.setEnabled(editable);
        if (!editable) {
            edit.setText("View");
        }
        choose.addActionListener(this);
        edit.addActionListener(this);
        remove.addActionListener(this);
        image_field.setEnabled(editable);
        // set file filter
    }
    
    public void actionPerformed(ActionEvent event) {
        try {
            if (event.getSource() == choose) {
                if(file_chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    field.set(new ImageIcon(file_chooser.getSelectedFile().getAbsolutePath()));
                    intoControl();
                }
            }
            if (event.getSource() == remove) {
                field.set(null);
                intoControl();
            }
            if (event.getSource() == edit){
                FieldImageLarge large = (FieldImageLarge)((FieldImage)field).getLarge();
                if (large.getLarge() != null) {
                    JImageEditor ed=new JImageEditor(large,editable);
                    ed.setLocationRelativeTo(getComponent());
                    ed.show();
                    image_field.setImage(field.get().getImage());
                    image_field.repaint();
                } else {
                    JOptionPane.showMessageDialog(null,"Use find to add a new image");
                }
            }
        } catch (Exception exception) {
            JOptionPane.showMessageDialog(null,exception.getMessage());
            Log.debug.out(exception);
        }
    }
    
    public DataPairFieldImage(Binder binder,BindImageInterface p_field,boolean editable){
        // default size to 100 X 100
        this(binder,p_field,new Dimension(100,100),editable);
    }
    
    public boolean isChanged(){
        return false;
    }
    
    public void intoControl(){
        if (field.get() != null) {
            image_field.setImage(field.get().getImage());
        } else {
            image_field.setImage(null);
        }
        image_field.setPreferredSize(preffered_size);
        image_field.repaint();
    }
    
    public void intoBindable(){
        //field.set(image_field.getImage());
    }
    
    public Bindable getBindable(){
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindImageInterface) p_field;
    }
    
    public void resetControl() {}
}

